package com.github.cakin.shiro.chapter19.web.controller;

import com.github.cakin.shiro.chapter19.entity.UrlFilter;
import com.github.cakin.shiro.chapter19.service.UrlFilterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @className: UrlFilterController
 * @description: UrlFilter的控制器
 * @date: 2020/5/28
 * @author: cakin
 */
@Controller
@RequestMapping("/urlFilter")
public class UrlFilterController {
    /**
     * 注入urlFilterService
     */
    @Autowired
    private UrlFilterService urlFilterService;

    /**
     * 功能描述：显示UrlFilter的列表
     *
     * @author cakin
     * @date 2020/5/28
     * @param model 返回给前端的数据
     * @return String 前端展示页面
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("urlFilterList", urlFilterService.findAll());
        return "urlFilter/list";
    }

    /**
     * 功能描述：创建UrlFilter的
     *
     * @author cakin
     * @date 2020/5/28
     * @param model 返回给前端的数据
     * @return String 前端展示页面
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showCreateForm(Model model) {
        model.addAttribute("urlFilter", new UrlFilter());
        model.addAttribute("op", "新增");
        return "urlFilter/edit";
    }

    /**
     * 功能描述：创建UrlFilter
     *
     * @author cakin
     * @date 2020/5/28
     * @param urlFilter 待创建的UrlFilter
     * @param redirectAttributes 重定向页属性
     * @return String 前端重定向页
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(UrlFilter urlFilter, RedirectAttributes redirectAttributes) {
        urlFilterService.createUrlFilter(urlFilter);
        redirectAttributes.addFlashAttribute("msg", "新增成功");
        return "redirect:/urlFilter";
    }
    /**
     * 功能描述：更新UrlFilter
     *
     * @author cakin
     * @date 2020/5/28
     * @param id 待更新的UrlFilter的Id
     * @param model 返回给前端的数据
     * @return String 前端展示页面
     */
    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
    public String showUpdateForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("urlFilter", urlFilterService.findOne(id));
        model.addAttribute("op", "修改");
        return "urlFilter/edit";
    }
    /**
     * 功能描述：更新UrlFilter
     *
     * @author cakin
     * @date 2020/5/28
     * @param urlFilter 待更新的UrlFilter
     * @param redirectAttributes 重定向页属性
     * @return String 前端重定向页
     */
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    public String update(UrlFilter urlFilter, RedirectAttributes redirectAttributes) {
        urlFilterService.updateUrlFilter(urlFilter);
        redirectAttributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/urlFilter";
    }
    /**
     * 功能描述：删除UrlFilter
     *
     * @author cakin
     * @date 2020/5/28
     * @param id 待删除的UrlFilter的Id
     * @param model 返回给前端的数据
     * @return String 前端展示页面
     */
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String showDeleteForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("urlFilter", urlFilterService.findOne(id));
        model.addAttribute("op", "删除");
        return "urlFilter/edit";
    }
    /**
     * 功能描述：删除UrlFilter
     *
     * @author cakin
     * @date 2020/5/28
     * @param id 待删除的UrlFilter的id
     * @param redirectAttributes 重定向页属性
     * @return String 前端重定向页
     */
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        urlFilterService.deleteUrlFilter(id);
        redirectAttributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/urlFilter";
    }

}
